package cn.com.greatwall.system.modules.oauth2.service.dto;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

import com.fasterxml.jackson.annotation.JsonIgnore;

import cn.com.greatwall.system.modules.system.service.dto.UserDto;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * @Author herw
 * @Time 2020-12-09 16:38:38
 * @Version 1.0
 * @Description: TODO(用一句话描述该文件做什么)
 */
@Getter
@AllArgsConstructor
public class JwtClientDto implements ClientDetails {
    private final OauthClientDetailsDto oauthClientDetailsDto;

    private final List<Long> dataScopes;
    
    @JsonIgnore
    private final List<GrantedAuthority> authorities;
    
    @Override
    public String getClientId() {
        return oauthClientDetailsDto.getId();
    }

    @Override
    public Set<String> getResourceIds() {
        Set<String> uri = new HashSet<String>();
        uri.addAll(Arrays.asList(oauthClientDetailsDto.getResourceIds().split(",")));
        return uri;
    }

    @Override
    public boolean isSecretRequired() {
        // TODO Auto-generated method stub
        return false;
    }

    @JsonIgnore
    @Override
    public String getClientSecret() {
        // TODO Auto-generated method stub
        return oauthClientDetailsDto.getClientSecret();
    }

    @Override
    public boolean isScoped() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public Set<String> getScope() {
        Set<String> uri = new HashSet<String>();
        uri.addAll(Arrays.asList(oauthClientDetailsDto.getScope().split(",")));
        return uri;
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        Set<String> uri = new HashSet<String>();
        uri.addAll(Arrays.asList(oauthClientDetailsDto.getAuthorizedGrantTypes().split(",")));
        return uri;
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        Set<String> uri = new HashSet<String>();
        uri.add(oauthClientDetailsDto.getWebServerRedirectUri());
        return uri;
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        // TODO Auto-generated method stub
        return oauthClientDetailsDto.getAccessTokenValidity();
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return oauthClientDetailsDto.getRefreshTokenValidity();
    }

    @Override
    public boolean isAutoApprove(String scope) {
        return oauthClientDetailsDto.getAutoapprove() == null ? false : Boolean.valueOf(oauthClientDetailsDto.getAutoapprove());
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        // TODO Auto-generated method stub
        return null;
    }

}
